//
//  FilterHeaderMenu.swift
//  ZYA
//
//  Created by 邱仙凯 on 2021/10/28.
//  Copyright © 2021 aowang. All rights reserved.
//

import UIKit
 
 // 头部筛选控件
class FilterHeaderMenu:UIView {
    
    var clickClosure:((Int)->())?
    private var titleArr = [String]()
    private  var btnArr = [KSHeaderItemView]()
    init(frame:CGRect,titleArr:[String],widthArr:[CGFloat]? = nil) {
        super.init(frame: frame)
        self.titleArr = titleArr
        self.btnArr.removeAll()
        var leading:CGFloat = 0
        for i in 0..<titleArr.count {
           
            var width:CGFloat = 0
            if widthArr.isSome {
                width = widthArr![i]
            } else {
                 width = KSGlobal.screenW/CGFloat(titleArr.count)
            }
            let btn1 = KSHeaderItemView(frame:CGRect(x:leading,y:0,width:width ,height:40))
            leading += width
            
            btn1.setTitle(titleArr[i])
            self.btnArr.append(btn1)
            let tap1 = UITapGestureRecognizer()
            tap1.numberOfTapsRequired = 1
            tap1.addTap { [unowned self,i] _ in
                if let clo = self.clickClosure {
                    clo(i)
                }
            }
            btn1.addGestureRecognizer(tap1)
            self.addSubview(btn1)
        }
    }
    
    func refreshTitle(index:Int,title:String) {
        self.titleArr[index] = title
        self.btnArr[index].setTitle(title)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
